<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useUserStore, useCommonStore } from '@/stores/index.js'
import { showLoadingToast } from 'vant'
import 'vant/lib/toast/style'
import api from "@/api/api";
import { isZxcxAPP } from "@/utils/zxcxWebkit.js";
import QRCode from "@/components/my/qrCode.vue";
import OrderCoupon from "@/components/serve/order_coupon.vue";

// 引入图片资源
import vipIcon from '@/assets/serve/vip.png'

// 响应式数据
const active = ref(0)
const loading = ref(false)
const loading2 = ref(true)
const finished = ref(true)
const couponList = ref([])
const showCouponList = ref(false)
const activeName = ref([])
const queryData = ref({})
const orderInfo = ref({})
const skuListInfo = ref({})
const storeInfo = ref({})
const carInfo = ref({})
const paySrc = ref("")
const show = ref(false)
const error = ref({
  show: false,
  message: "",
})

// DOM引用
const qrRef = ref(null)

// 路由和状态管理
const route = useRoute()
const router = useRouter()
const store = useUserStore()
const commonStore = useCommonStore()

// 获取车辆和用户信息
const carList = computed(() => store.carList)
const currentCarIndex = computed(() => commonStore.currentCarIndex)
const userData = computed(() => store.userData)

// 方法定义
const onClickLeft = () => {
  router.back()
}

const goVip = () => {
  router.push({
    path: "/vip",
  })
}

const goToPay = async () => {
  showLoadingToast({
    message: '加载中...',
    forbidClick: true,
  });
  let res = await api.pay.createTrade({
    settleId: orderInfo.value.settleId,
  })
  let res2 = await api.pay
    .pay({
      sn: res.sn,
    })
    .catch((err) => {
      console.log(err)
    })
  // Toast.clear()
  if (res2) {
    if (isZxcxAPP()) {
      window.location.href = res2
    } else {
      show.value = true
      paySrc.value = res2
    }
  } else {
    // Toast.fail({
    //   message: "支付失败",
    // })
  }
}

const getBuyDetail = async () => {
  showLoadingToast({
    message: '加载中...',
    forbidClick: true,
    duration: 1000,
  });
  orderInfo.value = await api.goods.buyNow(queryData.value)
    .catch((err) => {
      error.value.show = true
      error.value.message = err
    })
    .finally(() => {
      loading.value = false
    })
  skuListInfo.value = orderInfo.value.skuInfoList
  loading2.value = false
  storeInfo.value = await api.store.getStoreDetail({
    shopId: orderInfo.value.skuInfoList[0].shop.shopId,
  })
  // Toast.clear()
}

const showPopup = () => {
  showCouponList.value = true
}

const onLoad = () => {
  for (let i = 0; i < 10; i++) {
    couponList.value.push(couponList.value.length + 1)
  }

  // 加载状态结束
  loading.value = false

  // 数据全部加载完成
  if (couponList.value.length >= 0) {
    finished.value = true
  }
}

// 生命周期钩子

if (carList.value.length) {
  carInfo.value = carList.value[currentCarIndex.value]
}
if (route.query.cartType) {
  //购物车
} else {
  queryData.value.goodsId = route.query.id
  queryData.value.num = Number(route.query.num)
  queryData.value.skuId = route.query.skuId
  queryData.value.promotionId = route.query.promotionId
  queryData.value.promotionType = Number(route.query.promotionType)
  getBuyDetail()
}

</script>
<template>
  <div v-if="!loading2">
    <div v-if="error.show">
      <van-empty :description="error.message"></van-empty>
    </div>
    <div v-if="!loading && !error.show" class="order_wrapper">
      <van-nav-bar class="nav_bar" fixed title="确认订单" @click-left="onClickLeft" />
      <div class="store">
        <div class="top">
          <div class="name van-ellipsis">
            {{ storeInfo.title }}
          </div>
          <div class="rate">★ 5.0</div>
        </div>
        <div class="bottom">
          <div class="location">
            <div class="address_name">
              <van-icon name="location-o" color="#2e2f30" class="location_icon" />
              {{ storeInfo.address }}
            </div>
          </div>
          <div class="meter">{{ storeInfo.distance }}</div>
        </div>
      </div>
      <div v-if="Object.keys(carInfo).length > 0" class="order_info">
        <van-cell title="服务车辆" :border="false">
          <template #value>
            <div class="info_value">
              {{ carInfo.plateNumber }} {{ carInfo.brand }}
            </div>
          </template>
        </van-cell>
        <van-cell title="车主联系方式" :border="false">
          <template #value>
            <div class="info_value">{{ carInfo.ownerPhone }}</div>
          </template>
        </van-cell>
      </div>
      <div class="order_info">
        <div class="order_title">已选项目</div>
        <div class="order_item">
          <van-collapse v-model="activeName" :border="false">
            <van-collapse-item v-for="(v, i) in skuListInfo[0].skuList" :key="i" :title="v.goodsName" :name="i"
              :border="false">
              <template #right-icon>
                <div class="item_price">￥{{ v.totalPrice }}</div>
              </template>
              <template #default>
                <div class="goods_item">
                  <div class="left">
                    <van-image class="serve_pic" :src="v.pic" />
                    <div class="left_title">
                      {{ v.simpleSpecs.trim() }}
                    </div>
                  </div>
                  <div class="right">
                    <div class="num">x{{ v.num }}</div>
                  </div>
                </div>
              </template>
            </van-collapse-item>
          </van-collapse>
        </div>
      </div>
      <div v-if="!userData.isVip" class="vip">
        <div class="vip_top">
          <div class="vip_top_left">
            <van-image class="vip_icon" :src="vipIcon" />
            <span class="vip_tips">安心修大会员</span>
          </div>
          <div class="vip_top_right">
            <div class="btn" @click="goVip">立即开卡</div>
            <div class="price">￥199/年</div>
          </div>
        </div>
        <div class="vip_tip">
          <div class="vip_tip_text">
            开通会员立享折扣，预计本单可再省
            <span class="vip_tip_text_price">199.99</span>
            元
          </div>
        </div>
      </div>
      <div class="order_info">
        <div v-if="userData.isVip" class="dec">
          <img src="@/assets/store/vip.png" alt="" />
          <p>安心修大会员</p>
        </div>
        <div class="info-box">
          <van-cell center :border="false">
            <template #title>
              <div class="info_value">
                订单总价
                <span class="margin_left">共{{ skuListInfo[0].skuList.length }}项服务</span>
              </div>
            </template>
            <template #value>
              <div class="info_value red">￥{{ orderInfo.price }}</div>
            </template>
          </van-cell>
          <van-cell center :border="false">
            <template #title>
              <div class="info_value">工时费</div>
            </template>
            <template #value>
              <div class="info_value red">￥25</div>
            </template>
          </van-cell>
          <van-cell center is-link :border="false">
            <template #title>
              <div class="info_value">优惠券</div>
            </template>
            <template #value>
              <div class="info_value" @click="showPopup">暂无可用优惠券</div>
            </template>
          </van-cell>
          <van-cell v-if="userData.isVip" center :border="false">
            <template #title>
              <div class="info_value">安心修会员专属折扣(工时费8折)</div>
            </template>
            <template #value>
              <div class="info_value red">-¥6.99</div>
            </template>
          </van-cell>
          <van-cell center :border="false">
            <template #title>
              <div class="info_value">合计</div>
            </template>
            <template #value>
              <div class="info_value red">￥{{ orderInfo.price }}</div>
            </template>
          </van-cell>
        </div>
      </div>
      <div class="order_info">
        <van-cell center :border="false">
          <template #title>
            <div class="info_value">发票</div>
          </template>
          <template #value>
            <div class="info_value">请在到点服务时向门店索取</div>
          </template>
        </van-cell>
      </div>
      <div class="order_bottom" @click="goToPay">
        <div class="pay_btn">去支付</div>
      </div>
      <van-dialog v-model:show="show" show-cancel-button style="width: 80%;">
        <div style="display: flex; justify-content: center; padding: 27px 0 27px 0">
          <QRCode v-if="paySrc" ref="qrRef" :url="paySrc"></QRCode>
        </div>
      </van-dialog>
    </div>

    <van-popup v-model:show="showCouponList" round position="bottom" style="height: 80%; padding-top: 1px">
      <van-tabs v-model:active="active" color="#447ecc">
        <van-tab title="可用"></van-tab>
        <van-tab title="不可用"></van-tab>
      </van-tabs>
      <div style="padding: 7px">
        <van-list v-model:loading="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
          <OrderCoupon v-for="i in 3" :key="i"></OrderCoupon>
        </van-list>
      </div>

      <div class="button">不使用</div>
    </van-popup>
  </div>
</template>


<style lang="less" scoped>
.order_wrapper {
  min-height: 100vh;
  background: url("https://zxcxappimg.oss-cn-hangzhou.aliyuncs.com//zxcxmall/2025/05/29/44dea588672142d48f873df92c7b625e.jpg") no-repeat;
  background-color: #fff;
  background-size: contain;
  background-attachment: fixed;
  background-position: top;
  box-sizing: border-box;
  padding: 43px 17px 50px 17px;
  position: relative;

  .nav_bar {
    background: url("https://zxcxappimg.oss-cn-hangzhou.aliyuncs.com//zxcxmall/2025/05/29/44dea588672142d48f873df92c7b625e.jpg") no-repeat;
    background-size: cover;
    background-position: top center;

    :deep(.van-nav-bar__text) {
      color: #fff;
      margin-left: 7px;
      font-size: 17px;
    }

    :deep(.van-icon) {
      font-size: 17px;
      font-weight: bold;
      color: #fff;
    }

    :deep(.van-nav-bar__title) {
      color: #fff;
    }
  }

  :deep(.van-hairline--bottom::after) {
    border: unset;
  }

  .store {
    margin-top: 10px;
    padding: 20px 10px;
    background: #fff;
    border-radius: 7px;
    position: relative;

    .tips {
      padding: 5px 9px;
      background: linear-gradient(to bottom, #00bdf9, #015fea);
      border-top-left-radius: 7px;
      border-bottom-right-radius: 7px;
      position: absolute;
      left: 0;
      top: 0;
      font-size: 11px;
      color: #fff;
    }

    .top {
      display: flex;
      align-items: flex-start;
      margin-top: 3px;
      justify-content: space-between;
      padding: 3px 0;

      .name {
        font-size: 15px;
        color: #333;
        font-weight: bold;
        flex: 10;
      }

      .rate {
        font-size: 15px;
        color: #c10500;
        margin-left: 7px;
        flex: 2;
      }
    }

    .count {
      margin: 8px 0;
      font-size: 13px;
      color: #878787;
    }

    .bottom {
      display: flex;
      align-items: center;
      justify-content: space-between;

      .location {
        flex: 3;

        .address_name {
          font-size: 13px;
          margin-left: 5px;

          .location_icon {
            font-size: 17px;
          }
        }
      }

      .meter {
        font-size: 13px;
        margin-left: 7px;
      }
    }
  }

  .order_info {
    position: relative;
    padding: 15px 10px 15px 10px;
    background: #fff;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
    margin-top: 13px;
    border-radius: 7px;

    :deep(.van-cell) {
      padding: 3px 0;
    }

    .dec {
      display: flex;
      align-items: center;
      position: absolute;
      top: 0;
      left: 0;
      background: linear-gradient(to top, #6c6c6b, #353334);
      padding: 3px 5px;
      border-radius: 5px 0 5px 0;

      img {
        width: 23px;
        object-fit: contain;
      }

      p {
        color: #ecc085;
        font-size: 13px;
        margin: 0 3px;
      }
    }

    .info-box {
      margin-top: 17px;
    }

    .info_value {
      font-size: 13px;
      color: #333333;
      white-space: nowrap;
    }
  }

  .order_title {
    font-size: 15px;
    color: #323233;
  }

  .order_item {
    margin-top: 7px;
    margin-bottom: 8px;

    :deep(.van-cell) {
      background: #f8faff;
      padding: 7px 10px;
    }

    :deep(.van-collapse-item) {
      margin-bottom: 10px;
    }

    .item_price {
      font-size: 15px;
      color: #c10500;
      font-weight: bold;
    }

    :deep(.van-collapse-item__content) {
      padding: 10px 0;
    }

    .goods_item {
      display: flex;
      align-items: flex-start;
      justify-content: space-between;
      margin-bottom: 10px;

      .left {
        display: flex;
        align-items: flex-start;
        flex: 5;

        .serve_pic {
          flex: 1;
          width: 63px;
          min-width: 20px;
          aspect-ratio: 1;
        }

        &_title {
          flex: 3;
          font-size: 15px;
          color: #000000;
          margin-left: 7px;
        }
      }

      .right {
        text-align: right;
        flex: 1;

        .price {
          font-size: 13px;
          color: #000000;
        }

        .num {
          font-size: 13px;
          color: #eb0029;
        }
      }
    }

    .serve_item {
      margin-top: 10px;
      margin-bottom: 8px;

      .serve_title {
        margin-left: 7px;
        font-weight: bold;
      }

      .serve_info {
        font-size: 13px;
        color: #000000;

        &_price {
          margin-right: 5px;
        }
      }

      :deep(.van-cell) {
        background: transparent;
        padding: 5px 0;
      }
    }
  }

  .vip {
    padding: 3px 10px 10px 10px;
    margin-top: 10px;
    border-radius: 10px;
    background: linear-gradient(to right, #293a70, #575791);

    &_top {
      display: flex;
      align-items: center;
      justify-content: space-between;

      &_left {
        display: flex;
        align-items: center;

        .vip_icon {
          width: 33px;
          height: 30px;
        }

        .vip_tips {
          font-size: 13px;
          color: #fadab0;
          margin-left: 5px;
        }
      }

      &_right {
        display: flex;
        align-items: center;

        .btn {
          padding: 0 10px;
          height: 27px;
          background: #d49a50;
          color: #fff;
          line-height: 27px;
          border-radius: 333px;
          font-size: 15px;
        }

        .price {
          color: #fff;
          font-size: 15px;
          margin-left: 3px;
        }
      }
    }

    &_tip {
      padding: 5px 3px;
      background: #fff;
      margin-top: 7px;

      &_text {
        font-size: 13px;
        color: #000;

        &_price {
          font-size: 15px;
          color: #eb0029;
        }
      }
    }
  }

  .vip_rule {
    font-size: 12px;
    color: #333;
    text-align: center;
    margin-top: 8px;

    .rule_icon {
      font-size: 13px;
    }
  }

  .order_bottom {
    margin-top: 33px;
    box-sizing: border-box;
    width: 100%;
    display: flex;
    justify-content: center;

    .pay_btn {
      height: 33px;
      line-height: 33px;
      border-radius: 17px;
      background: #e73353;
      color: #fff;
      text-align: center;
      font-size: 15px;
      padding: 0 40px;
    }
  }

  .red {
    color: #eb0029 !important;
  }

  .margin_left {
    margin-left: 10px;
  }

  :deep(.van-cell__right-icon) {
    display: flex;
    align-items: center;
  }
}

.button {
  width: 70%;
  font-size: 17px;
  color: white;
  background-color: #3985f7;
  padding: 7px 0;
  margin: 0 auto;
  text-align: center;
  border-radius: 21px;
}
</style>
